package main

type ListNode struct {
	Val  int
	Next *ListNode
}

func getLenAndGetTail(head *ListNode) (int, *ListNode) {
	size := 0

	if head == nil {
		return size, head
	}

	size++

	for head.Next != nil {
		size++
		head = head.Next
	}

	return size, head
}

func rotateRight(head *ListNode, k int) *ListNode {
	size, tail := getLenAndGetTail(head)

	if size == 0 {
		return head
	}
	k %= size

	if k == 0 {
		return head
	}

	temp := head
	for i := 1; i < size-k; i++ {
		temp = temp.Next
	}

	newHead := temp.Next
	temp.Next = nil
	tail.Next = head

	return newHead
}
